<?php

class SetariClient {
	public $id_setariclient;
	public $id_client;
	public $cale_banner;
	public $termeni;
	public $antet_mail;
	public $zile_scadenta_sol;
	public $zile_scadenta_exec;
	public $zile_alarma_plati;
	public $zile_alarma_incasari;
	
	public $zile_alarma_incarcari;
	public $zile_alarma_descarcari;
	
	function __construct($id_client, $cale_banner = null, $termeni = null, $antet_mail = null, $zile_scadenta_sol = null, $zile_scadenta_exec = null, $zile_alarma_plati = null, $zile_alarma_incasari = null, $zile_alarma_incarcari = null, $zile_alarma_descarcari = null) {
		$this->id_client = $id_client;
		$this->cale_banner = $cale_banner;
		$this->termeni = $termeni;
		$this->antet_mail = $antet_mail;
		$this->zile_scadenta_sol = $zile_scadenta_sol;
		$this->zile_scadenta_exec = $zile_scadenta_exec;
		$this->zile_alarma_plati = $zile_alarma_plati;
		$this->zile_alarma_incasari = $zile_alarma_incasari;
		$this->zile_alarma_incarcari = $zile_alarma_incarcari;
		$this->zile_alarma_descarcari = $zile_alarma_descarcari;
	}
	
	public function queryInsertSetariClient($db) {
		$query = $db->prepare("INSERT INTO `setari_client`  ( 	`id_client` , 				`cale_banner` , 
																`termeni` , 				`antet_mail`,
	                        		        					`zile_scadenta_sol` ,    	`zile_scadenta_exec` , 
		                        	        					`zile_alarma_plati` , 		`zile_alarma_incasari`,
		                        	        					`zile_alarma_incarcari`,	`zile_alarma_descarcari`                     
		                               	) 
		            			VALUES  (						:id_client,  				:cale_banner, 
		            											:termeni,   				:antet_mail, 
	                      										:zile_scadenta_sol, 		:zile_scadenta_exec, 
		                      									:zile_alarma_plati,			:zile_alarma_incasari,
		                      									:zile_alarma_incarcari,		:zile_alarma_descarcari             						                     
		                      			)");
		
		//var_dump($this);
		$query->bindParam(':id_client', 			($this->id_client));	
		$query->bindParam(':cale_banner', 			($this->cale_banner));
		$query->bindParam(':termeni', 				($this->termeni));
		$query->bindParam(':antet_mail', 			($this->antet_mail));
		$query->bindParam(':zile_scadenta_sol', 	($this->zile_scadenta_sol));
		$query->bindParam(':zile_scadenta_exec', 	($this->zile_scadenta_exec));
		$query->bindParam(':zile_alarma_plati', 	($this->zile_alarma_plati));
		$query->bindParam(':zile_alarma_incasari', 	($this->zile_alarma_incasari));
		
		$query->bindParam(':zile_alarma_incarcari',	$this->zile_alarma_incarcari);
		$query->bindParam(':zile_alarma_descarcari',$this->zile_alarma_descarcari);
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();

			throw new QueryInsertSetariException("Error inserting SetariClient for client id =  " . id_client . $errInfo);
		}
		
  		$this->id_setariclient = (int) $db->lastInsertId();
  		$query->closeCursor();
		return $this->id_setariclient; 		//return the setari_client's id
	}
	
	public function queryUpdateSetariClient($db) {
		$query = $db->prepare("UPDATE `setari_client` SET
							`id_client` = :id_client , `cale_banner` = :cale_banner, 
							`termeni` = :termeni, `antet_mail` = :antet_mail,
        					`zile_scadenta_sol` = :zile_scadenta_sol, `zile_scadenta_exec` = :zile_scadenta_exec, 
        					`zile_alarma_plati` = :zile_alarma_plati , `zile_alarma_incasari` = :zile_alarma_incasari,
        					`zile_alarma_incarcari` = :zile_alarma_incarcari,	`zile_alarma_descarcari` = :zile_alarma_descarcari                     
							WHERE id_client = :id_client");	        					

		$query->bindParam(':id_client', 			($this->id_client));			
		$query->bindParam(':cale_banner', 			($this->cale_banner));
		$query->bindParam(':termeni', 				stripslashes($this->termeni));
		$query->bindParam(':antet_mail', 			($this->antet_mail));
		$query->bindParam(':zile_scadenta_sol', 	($this->zile_scadenta_sol));
		$query->bindParam(':zile_scadenta_exec', 	($this->zile_scadenta_exec));
		$query->bindParam(':zile_alarma_plati', 	($this->zile_alarma_plati));
		$query->bindParam(':zile_alarma_incasari', 	($this->zile_alarma_incasari));
		
		$query->bindParam(':zile_alarma_incarcari',	$this->zile_alarma_incarcari);
		$query->bindParam(':zile_alarma_descarcari',$this->zile_alarma_descarcari);										
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new QueryUpdateStareException("Error updating SetariClient for client id =  " . id_client . $errInfo);
		}
		
  		$this->id_setariclient = (int) $db->lastInsertId();
  		$query->closeCursor();
		return $this->id_setariclient; 		//return the setari_client's id
	}
		
  	public static function &queryLoadSetariClient($db, $id_client) {
        $query = $db->prepare("SELECT *
            					FROM setari_client
            					WHERE id_client = :id_client");
        
        $query->bindParam(':id_client', $id_client);                    					
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
        	$query->closeCursor();
        	throw new QueryLoadSetariException("Failed loading setari client for client id " . $id_client . $errInfo);            
        }

    	$data = $query->fetch(PDO::FETCH_ASSOC);        
    	
        if (!isset($data['id'])) {
        	$query->closeCursor();
        	throw new QuerySettingsNotExistException("Failed loading setari client for client id " . $id_client . " reason: id not existing");                    	               	     	
        }
                    
        $setari_client = new SetariClient($data['id_client'], $data['cale_banner'], $data['termeni'], $data['antet_mail'], $data['zile_scadenta_sol'] 
        							, $data['zile_scadenta_exec'], $data['zile_alarma_plati'], $data['zile_alarma_incasari']
        							, $data['zile_alarma_incarcari'], $data['zile_alarma_descarcari']);
        $setari_client->id_setariclient = (int) $data['id'];
        $setari_client->id_client = (int) $data['id_client'];
        $query->closeCursor();
        return $setari_client;
    }	    
    
    function queryDeleteSetariClient($db) {
      	$query = $db->prepare("DELETE FROM setari_client WHERE id = :id_setariclient");      	
      	$query->bindParam(':id_setariclient', $this->id_setariclient);                    				
      		
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
        	$query->closeCursor();
        	throw new QueryDeleteSetariException("Failed deleting setari client with id: " . $this->id_setariclient . $errInfo);
        }
	  	
        $query->closeCursor();
    	return CT_RESULT_OK;      	
    }
    
    //deletes setari_client by id; does not require an object.... STATIC
    public static function deleteSetariClientById($db, $id_setariclient) {
    	$query = $db->prepare("DELETE FROM setari_client WHERE id = :id_setariclient");
    	$query->bindParam(':id_setariclient', $id_setariclient);                    				
    	    	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
    		throw new QueryDeleteSetariException("Failed deleting setari client with id: " . $id_setariclient . $errInfo);  	
      	}
      	
    	$query->closeCursor();	
      	return CT_RESULT_OK;      	
    }
}

?>